
#include "spider_filter_rules.h"



pcre *sfr_pcre_compile(char* pattern)
{
    pcre *re;
    int erroffset;
    const char *error;

    re = pcre_compile( pattern, 0, &error, &erroffset, NULL);
    if (NULL == re)
    {
        printf("pcre_compile error: %s \n", error);
        return NULL;
    }
    return re;
}



http_header_t *http_header_init()
{
    http_header_t *h = (http_header_t *)malloc(sizeof(http_header_t));

    if (NULL != h)
    {
        memset(h, 0, sizeof(http_header_t));
    }

    return h;
}


void http_header_release(http_header_t *h)
{

    if (NULL != h)
    {
        safe_free(h->host);
        safe_free(h->url);
        safe_free(h->refer);
        free(h);
    }

    return ;
}



int http_rule_pattern(http_header_t *h, spider_filter_rule_t *sfr, int rule_num)
{
    int i = 0;
    int rc;

    int vector[TS_MAX_URL_VECTOR];

    if (NULL == h || NULL == sfr)
    {
        return -1;
    }
    
    for (i = 0; i < rule_num; i++)
    {        
        if (h->url)
        {

            //printf("-->rule_num = %d, sfr[i].url_re = %p, sfr[i].url_regex = %s,\n-->h->url = %s \n-->url_len = %d\n", rule_num, sfr[i].url_re ,sfr[i].url_regex ,h->url,strlen(h->url));
            
            rc = pcre_exec(sfr[i].url_re, NULL, h->url, strlen(h->url), 0, 0, vector, TS_MAX_TITLE_VECTOR);

            //printf("===> rc = %d\n", rc);
            
            if (rc < 0)
            {
                //printf("pcre_exec error in title! \n");
                continue ;
            }
            else
            {
                //printf("pcre_exec OK in title! \n");
                return i;
            }
        }
    }

    if (i == rule_num)
    {
        //printf(" regular partten fail ! \n");
        return -1;
    }
}


char *http_search_urlinfo_in_buffer(pcre *re, char* buffer, const char **url, char **uriptr)
{

    int erroffset;
    const char *error;
    int rc;
    int vector[TS_MAX_URL_VECTOR];
    size_t offset_out;

    if (NULL == re)
    {
       printf("pcre_compile error:  in buffer %s \n", error);
        return NULL;
    }
    
    rc = pcre_exec( re, NULL, buffer, strlen(buffer), 0, 0, vector, TS_MAX_URL_VECTOR);
    
    if (rc < 0)
    {
        printf( "pcre_exec error in buffer! \n");
        return NULL;
    }

    *uriptr = buffer+vector[0];

    offset_out = vector[1];
    pcre_get_named_substring(re, buffer, vector, rc, "url", url);

    return buffer+offset_out;
}



http_header_t *get_http_header(char *url_data)
{
    http_header_t *h;

    int erroffset;
    const char *error;
    int rc;
    int vector[TS_MAX_CHARSET_VECTOR];

    char *url;
    char *host;


    char *http = "http://";

    static pcre *re = NULL;

    h = http_header_init();
    
    if (NULL == h)
    {
        return NULL;
    }

    if (NULL == re)
    {
        re = pcre_compile(PATTERN_URLINFO, 0, &error, &erroffset, NULL);
        if (NULL == re)
        {
            printf("pcre_compile error: %s \n", error);
            return NULL;
        }
    }


    //printf(" url : %s \n",url_data);
    
    rc = pcre_exec( re, NULL, url_data, strlen(url_data), 0, 0, vector, TS_MAX_CHARSET_VECTOR);
    if (rc < 0)
    {
        //printf("pcre_exec error in charset! \n");
        return NULL;
    }

    //printf(" url pcre OK \n");
    
    pcre_get_named_substring(re, url_data, vector, rc, "url", (const char **)&url);

    pcre_get_named_substring(re, url_data, vector, rc, "host", (const char **)&host);

    if (NULL != url)
    {
         rc = strncasecmp(url, "http://", sizeof("http://")-1);

         if (0 != rc && NULL != host)
         {
            h->url = (char *)malloc(strlen(http) + strlen(url) + strlen(host) + 1);

            sprintf(h->url, "%s%s%s", http, host, url);
            //printf(" 111111111 h->url = %s \n", h->url);
         }
         else
         {
            h->url = (char *)malloc(strlen(url) + 1); 
	    sprintf(h->url, "%s", url);

            //printf(" 222222222 h->url = %s \n", h->url);
         }
    }
    else
    {
        h->url = NULL;
    }



    if (NULL != host)
    {
        h->host = strdup(host);
    }
    else
    {
        h->host = NULL;
    }

    if (url) pcre_free_substring(url);
    if (host) pcre_free_substring(host);

    return h;
}



